package com.tbs.parkingsaleapp.UI.UserCenterPage;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.tbs.parkingsaleapp.Http.AsycHttp;
import com.tbs.parkingsaleapp.Http.JsonData.Houseinfo;
import com.tbs.parkingsaleapp.Http.JsonData.LikeInfo;
import com.tbs.parkingsaleapp.Http.JsonData.Message;
import com.tbs.parkingsaleapp.Http.Listenner.MessageHttper;
import com.tbs.parkingsaleapp.R;
import com.tbs.parkingsaleapp.SystemManager.SystemDataMananger;
import com.tbs.parkingsaleapp.UI.Adapters.MsgAdapter;
import com.tbs.parkingsaleapp.UI.UpdateLike.UpdateLikeActivity;
import com.tbs.parkingsaleapp.UI.Widget.TraitShowController;


public class UserCenterFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void UpdateMessageList(Message[] messages)
    {
        lv.setAdapter(new MsgAdapter(messages,lv));
    }
    ListView lv;
    public static void UpdateLike()
    {
        TraitShowController showController=new TraitShowController(root,SystemDataMananger.getLogined().getLikeInfo());
    }
    private static View root;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        root=inflater.inflate(R.layout.fragment_user_center, container, false);
        if(SystemDataMananger.isLogined())
        {
            UpdateLike();
            ListView house=root.findViewById(R.id.uc_ownedhouse);
            ArrayAdapter<String> houseadp=new ArrayAdapter<String>(getContext(),R.layout.support_simple_spinner_dropdown_item);
            int i=1;
            for (Houseinfo houseinfo:SystemDataMananger.getLogined().getHouseinfo()
                 ) {
                houseadp.add(String.format("%d.%s %d号楼%d室",i++,houseinfo.getLocation().getName(),houseinfo.getHouse().getNumber(),houseinfo.getHouse().getHouse()));
            }
            house.setAdapter(houseadp);
            TextView textView=root.findViewById(R.id.uc_userinfo);
            textView.setText(String.format("用户名:%s\n身份证：%s \n手机号:%s",SystemDataMananger.getLogined().getUserinfo().getUserName(),SystemDataMananger.getLogined().getUserinfo().getId(),SystemDataMananger.getLogined().getUserinfo().getPhone()));
            Button btn=root.findViewById(R.id.updateMy);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent();
                    intent.setClass(getContext(), UpdateLikeActivity.class);
                    startActivityForResult(intent,UpdateLikeActivity.UPDATED);
                }
            });
            lv=root.findViewById(R.id.msg_ls);
            lv.setDivider(getContext().getDrawable(R.drawable.blank));
            lv.setDividerHeight(25);
            MessageHttper httper=new MessageHttper(this);
            AsycHttp<String,Message[]> http=new AsycHttp<String,Message[]>().setBackgroundAction(httper).setResultAction(httper).setContext(getContext());
            http.execute(SystemDataMananger.getLogined().getUserinfo().getId());

        }
        else
        {
            TextView textView=root.findViewById(R.id.uc_userinfo);
            textView.setText("您尚未登录");
            Button btn=root.findViewById(R.id.updateMy);
            btn.setEnabled(false);
        }
        return root;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode== UpdateLikeActivity.UPDATED &&resultCode==UpdateLikeActivity.UPDATED)
        {
            SystemDataMananger.getLogined().setLikeInfo(LikeInfo.IntToTriat(data.getIntExtra(UpdateLikeActivity.UPDATEKEY, LikeInfo.TraitToInt(SystemDataMananger.getLogined().getLikeInfo()))));
            UserCenterFragment.UpdateLike();
        }
    }
}